package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import dao.ArticleDao;
import entity.AType;
import entity.Article;

public class ArticleService {

	static List<Article> articles = new ArrayList<Article>();
	
	static {
		Article article = new Article();
		article.setId("1");
		article.setContent("content1");
		article.setPostTime(new Date());
		article.setIp("ip1");
		
		AType aType = new AType();
		aType.setId("1");
		aType.setName("type1");
		
		article.setaType(aType);

		articles.add(article);
		
		Article article2 = new Article();
		article2.setId("2");
		article2.setContent("content2");
		article2.setPostTime(new Date());
		article2.setIp("ip2");
		
		AType aType2 = new AType();
		aType2.setId("2");
		aType2.setName("type2");
		
		article2.setaType(aType2);
		
		articles.add(article2);
	}
	
	public  List<Article> list(){
//		ArticleDao dao = new ArticleDao();
//		return dao.findAll();
		return articles;
	}
	
	public  Article findById(String id){
		return articles.stream().filter(article->article.getId().equals(id)).findFirst().orElse(null);
	}
	
	public  void save(Article article){
		articles.add(article);
		//记录操作日志....logDao.save()
	}
	
	public  void update(Article article){
		for(int index = articles.size() -1;index>0;index--) {
			if( articles.get(index).getId().equals(article.getId())) {
				articles.get(index).setContent(article.getContent());
				break;
			}
		}
	}
	
	public  void delete(String id){
		for(int index = articles.size() -1;index>=0;index--) {
			if( articles.get(index).getId().equals(id)) {
				articles.remove(index);
			}
		}
	}
}
